package com.probiz.estore.common.model.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.probiz.estore.common.model.product.base.ProductTbl;
import com.probiz.estore.core.util.ContextUtil;
import com.probiz.estore.customer.service.ConsultManager;
import com.probiz.estore.other.service.TagManager;

/**
 * Model class for Product. Add not database mapped fileds in this class.
 * 商品
 */
public class Product extends ProductTbl {
	
	public static final Short PRODUCTKIND_PRODUCT = 1; //普通商品
	public static final Short PRODUCTKIND_PACKAGE = 2; //商品包
	
	public final static Integer DEFAULT_SORTORDER = 10; //默认排序值
	
	public final static String PRODUCT_CODE_DEFAULT_VALUE = "NULL";
	public final static Short STATUS_ALL = 100; //状态不限
	
	public final static Short STATUS_LOGICAL_DELETED = -1; //逻辑删除
	public final static Short STATUS_NOTACTIVE = 0; //下架
	public final static Short STATUS_ACTIVE = 1; //上架
	
	
	public final static Short ORDERBY_PRICE = 1; //价格从低到高
	public final static Short ORDERBY_DPRICE = 2; //价格从高到低
	public final static Short ORDERBY_PRODUCTNAME = 3; //产品名称升序
	public final static Short ORDERBY_DPRODUCTNAME = 4; //产品名称降序
	public final static Short ORDERBY_UPDATETIME = 5; //更新时间从晚到早
	public final static Short ORDERBY_DUPDATETIME = 6; //更新时间从早到晚
	public final static Short ORDERBY_BUYCOUNT = 7; //购买数量从少到多
	public final static Short ORDERBY_DBUYCOUNT = 8; //购买数量从多到少
	public final static Short ORDERBY_RATE = 9; //用户评分从低到高
	public final static Short ORDERBY_DRATE = 10; //用户评分从高到低
	public final static Short ORDERBY_SHELVETIME = 11; //上架时间从晚到早
	public final static Short ORDERBY_DSHELVETIME = 12; //上架时间从早到晚


    /**
     * Default Empty Constructor for class Product
     */
    public Product() {
        super();
    }

    /**
     * Default Key Fields Constructor for class Product
     * @param in_productId 商品id
     */
    public Product(Integer in_productId) {
        super(in_productId);
    }


    /**
     * 构建简单的Json对象主要用于选择器
     * @return json String
     */
    public String getJsonObject() {
        JSONObject jsonProduct = new JSONObject();
        jsonProduct.put("productId", this.productId);
        jsonProduct.put("productName", this.productName);
        jsonProduct.put("productCode", this.productCode);
        Map<String, Object> defaultProductSku = new HashMap<String, Object>();
        defaultProductSku.put("productSkuId", this.getDefaultProductSku() == null ? "" : this.getDefaultProductSku().getProductSkuId());
        defaultProductSku.put("productSkuCode", this.getDefaultProductSku() == null ? "" : this.getDefaultProductSku().getProductSkuCode());
        defaultProductSku.put("price", this.getDefaultProductSku() == null ? "" : this.getDefaultProductSku().getPrice());
        jsonProduct.put("defaultProductSku", defaultProductSku);
        Map<String, Object> brand = new HashMap<String, Object>();
        brand.put("brandId", this.getBrand() == null ? "" : this.getBrand().getBrandId());
        brand.put("brandName", this.getBrand() == null ? "" : this.getBrand().getBrandName());
        jsonProduct.put("brand", brand);
        return jsonProduct.toString();
    }

    public List<ProductMedia> getAccessories(){
    	List<ProductMedia> accessories = new ArrayList<ProductMedia>();
    	for(ProductMedia productMedia: productMedias){
    		if(productMedia.getMediaType().equals(ProductMedia.TYPE_ACCESSORY)){
    			accessories.add(productMedia);
    		}
    	}
    	return accessories;
    }
    
    public List<ProductMedia> getProductImages(){
    	List<ProductMedia> productImages = new ArrayList<ProductMedia>();
    	for(ProductMedia productMedia: productMedias){
    		if(productMedia.getMediaType().equals(ProductMedia.TYPE_IMAGE)){
    			productImages.add(productMedia);
    		}
    	}
    	return productImages;
    }
    /**
     * 获得标签名称
     * @return
     */
    public String getTagNames(){
    	TagManager tagManager = (TagManager) ContextUtil.getSpringBeanById("tagManager");
    	return tagManager.getTagNames(this);
    }
    /**
     * 获得标签数组
     * @return
     */
    public String[] getTagNameArray(){
    	TagManager tagManager = (TagManager) ContextUtil.getSpringBeanById("tagManager");
    	return tagManager.getTagNameArray(this);
    }
    
    public Long getConsultCount(){
    	if(null != productId){
	    	ConsultManager consultManager = (ConsultManager)ContextUtil.getSpringBeanById("consultManager");
	    	return consultManager.getOnShowConsultCountByProductId(productId);
    	}else{
    		return null;
    	}
    }

    
    /**
	 * 是否一个实体商品
	 * @return
	 */
	public boolean getIsPhysicalProduct(){
		if(getProductType().getKind().equals(ProductType.PRODUCT_KIND_ENTITY)){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 是否一个虚拟商品
	 * @return
	 */
	public boolean getIsDigitalProduct(){
		if(!getProductType().getKind().equals(ProductType.PRODUCT_KIND_ENTITY)){
			return true;
		}else{
			return false;
		}
	}
	
	

}
